﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Code.Models;
using Code.Commen;
using Code.BLL;
using QianBuo.SchemeTool.Forms;
using System.Security.Cryptography;
using System.Security;

namespace QianBuo.SchemeTool
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.Icon = Resources.switchuser;
            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                CheckReg();
            }
            catch (System.Data.Common.DbException)
            {
                MessageBox.Show("数据库连接失败");
                System.Environment.Exit(0);
            }
        }

        private void CheckReg()
        {
            var SM = new SiteManage();
            var key = Commen.Management.GetRegID();

            var op = SM.Select(s => s.Where(p => p.Key == "Site_Other_RegistSchemeTool").Select(p => p.Value).FirstOrDefault()) ?? string.Empty;
            var regs = op.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
            var reg = regs.FirstOrDefault(p => p.StartsWith(key, StringComparison.CurrentCultureIgnoreCase));

            var salt = Salt.Keys.GetMd5Salt();
            var md5 = MD5.Create();
            var pwd =md5.ComputeHash(key, salt).ToUpper();

            if (reg == null || !reg.EndsWith(pwd, StringComparison.CurrentCultureIgnoreCase))
            {
                DialogResult dr = MessageBox.Show("很抱歉，您还未注册！\r\n是否立即注册？", "注册提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dr == DialogResult.Yes)
                {
                    var f = new RegistForm(key,pwd);
                    dr = f.ShowDialog();
                }

                if (dr == DialogResult.OK)
                {
                    var item = SM.Select(s => s.Where(p => p.Key == "Site_Other_RegistSchemeTool").FirstOrDefault());
                    int rs = 0;
                    if (item == null)
                        rs = SM.Insert<Option_Site>(new { Key = "Site_Other_RegistSchemeTool", Value = key + "#" + pwd });
                    else
                        rs = SM.Update<Option_Site>(new { Key = "Site_Other_RegistSchemeTool" }, new { Value = item.Value + "&" + key + "&" + pwd });
                    if (rs < 1)
                        MessageBox.Show("注册失败！");
                }
                else
                { 
                    MessageBox.Show("程序还未注册，即将退出！");
                    System.Environment.Exit(0);
                }
            }
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuScheme_Click(object sender, EventArgs e)
        {
            var f = Application.OpenForms.Cast<Form>().FirstOrDefault(p => p.Name == "SchemeForm") as SchemeForm;
            if (f == null)
            {
                f = new SchemeForm();
                f.MdiParent = this;
                f.Show();
            }
            f.Focus();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            var rs = MessageBox.Show("您确定要退出吗?", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == DialogResult.No)
                e.Cancel = true;
        }
    }
}
